const db = require('../db/index')
const bcrypt = require('bcryptjs')
const jwt = require("jsonwebtoken")
const config = require("../config.js")


/**
 * 获取产品列表
 * @route get /pro/getProduct  
 * @summary 获取产品列表
 * @group 产品模块
 * @param {string} pageNum.query.required - 页数
 * @param {string} pageSize.query.required - 页大小
 * @param {string} product_id.query.required - 产品id
 * @returns {object} 200 - {code:200,msg:'ok',data:null}
 * @returns {Error}  default - {code:500,msg:'error',data:null}
 * @security JWT
 */
exports.getProduct = (req, res) => {
    let pageNum = req.query.pageNum;
    let pageSize = req.query.pageSize;
    let likeStr = ``;
    if (req.query.product_id) {
        likeStr = ` and product_id like '%${req.query.product_id}%'`;
    }
    const sql = `select * from product where product_inwarehouse_number > 0` + likeStr + `  limit ${(pageNum - 1) * pageSize} ,${pageSize}`;
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        const sql1 = `select count(*) as total from product where product_inwarehouse_number > 0` + likeStr;
        db.query(sql1, (err, results1) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            res.send({
                // 状态
                status: 0,
                // 状态描述，判断 err 是 错误对象 还是 字符串
                message: '查询成功',
                data: {
                    list: results,
                    total: results1[0].total
                }
            })
        })

    })

}


exports.addProduct = (req, res) => {
    const product_create_time = new Date().toLocaleString();
    const product_all_price = req.body.product_inwarehouse_number * 1 * req.body.product_single_price * 1;
    let data = {
        product_id: req.body.product_id,
        product_name: req.body.product_name,
        product_category: req.body.product_category,
        product_unit: req.body.product_unit,
        product_inwarehouse_number: req.body.product_inwarehouse_number,
        product_single_price: req.body.product_single_price,
        product_all_price,
        product_create_person: req.body.product_create_person,
        in_memo: req.body.in_memo,
        product_create_time
    }
    const sql = 'insert into product set ?';
    db.query(sql, data, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // SQL 语句执行成功，但影响行数不为 1
        if (results.affectedRows !== 1) {
            return res.send({ status: 1, message: '新增产品失败' })
        }
        res.send({ status: 0, message: '新增产品成功！' })
    })
}

exports.editProduct = (req, res) => {
    const product_update_time = new Date().toLocaleString();
    const product_all_price = req.body.product_inwarehouse_number * 1 * req.body.product_single_price * 1;
    let data = {
        // product_id: req.body.product_id,
        product_name: req.body.product_name,
        product_category: req.body.product_category,
        product_unit: req.body.product_unit,
        product_inwarehouse_number: req.body.product_inwarehouse_number,
        product_single_price: req.body.product_single_price,
        product_all_price,
        // product_create_person: req.body.product_create_person,
        in_memo: req.body.in_memo,
        product_update_time
    }
    const sql = 'update  product set ? where id =? ';
    db.query(sql, [data, req.body.id], (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // SQL 语句执行成功，但影响行数不为 1
        if (results.affectedRows !== 1) {
            return res.send({ status: 1, message: '修改产品失败' })
        }
        res.send({ status: 0, message: '修改产品成功！' })
    })
}

exports.deleteProduct = (req, res) => {
    const sql = 'delete from  product  where id =? ';
    db.query(sql, [req.query.id], (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // SQL 语句执行成功，但影响行数不为 1
        if (results.affectedRows !== 1) {
            return res.send({ status: 1, message: '删除产品失败' })
        }
        res.send({ status: 0, message: '删除产品成功！' })
    })
}



exports.getApplyProduct = (req, res) => {
    let pageNum = req.query.pageNum;
    let pageSize = req.query.pageSize;
    let likeStr = ``;
    if (req.query.product_out_id) {
        likeStr = ` and product_out_id like '%${req.query.product_out_id}%'`;
    }
    const sql = `select * from product where product_out_status not in ('同意')` + likeStr + `  limit ${(pageNum - 1) * pageSize} ,${pageSize}`;
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        const sql1 = `select count(*) as total from product where product_out_status not in ('同意')` + likeStr;
        db.query(sql1, (err, results1) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            res.send({
                // 状态
                status: 0,
                // 状态描述，判断 err 是 错误对象 还是 字符串
                message: '查询成功',
                data: {
                    list: results,
                    total: results1[0].total
                }
            })
        })
    })
}

exports.applyProduct = (req, res) => {
    let product_out_apply_date = new Date().toLocaleString();
    let product_out_status = '申请出库'
    let data = {
        product_out_id: req.body.product_out_id,
        product_out_number: req.body.product_out_number,
        product_out_price: req.body.product_out_price,
        product_out_apply_person: req.body.product_out_apply_person,
        apply_memo: req.body.apply_memo,
        product_out_apply_date,
        product_out_status
    }
    const sql = 'update product set ? where id =?';
    db.query(sql, [data, req.body.id], (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // SQL 语句执行成功，但影响行数不为 1
        if (results.affectedRows !== 1) {
            return res.send({ status: 1, message: '申请失败' })
        }
        res.send({ status: 0, message: '申请成功！' })
    })

}

exports.auditProduct = (req, res) => {
    const product_out_status = req.body.product_out_status;
    const product_audit_time = new Date().toLocaleString();
    if (product_out_status == '同意') {
        let data = {
            product_out_id: req.body.product_out_id,
            product_out_number: req.body.product_out_number,
            product_out_price: req.body.product_out_price,
            product_out_apply_person: req.body.product_out_apply_person,
            product_audit_time,
            product_out_audit_person: req.body.product_out_audit_person,
            audit_memo: req.body.audit_memo,
        }
        const sql = 'insert into outproduct set ?';
        db.query(sql, data, (err, results) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            // SQL 语句执行成功，但影响行数不为 1
            if (results.affectedRows !== 1) {
                return res.send({ status: 1, message: '审核失败' })
            }
            let product_inwarehouse_number = req.body.product_inwarehouse_number - req.body.product_out_number;
            let product_all_price = product_inwarehouse_number * 1 * req.body.product_single_price * 1;
            const sql1 = `update product set product_inwarehouse_number =?,product_all_price= ?, product_out_id = null, product_out_number= null,product_out_price = null,product_out_apply_person =null,product_out_apply_date=null,apply_memo =null,product_out_status = '同意' where id = ?`;
            db.query(sql1, [product_inwarehouse_number, product_all_price, req.body.id], (err, results) => {
                // 执行 SQL 语句失败
                if (err) return res.cc(err);
                // SQL 语句执行成功，但影响行数不为 1
                if (results.affectedRows !== 1) {
                    return res.send({ status: 1, message: '审核失败' })
                }
                res.send({ status: 0, message: '审核成功！' })
            })
        })
    } else if (product_out_status == '禁止') {
        const sql1 = `update product set audit_memo = ?,product_aut_audit_person = ? where id = ?`;
        db.query(sql1, [req.body.audit_memo, req.body.product_aut_audit_person, req.body.id], (err, results) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            // SQL 语句执行成功，但影响行数不为 1
            if (results.affectedRows !== 1) {
                return res.send({ status: 1, message: '审核失败' })
            }
            res.send({ status: 0, message: '禁止成功！' })
        })
    }
}


exports.getOutproduct = (req, res) => {
    let pageNum = req.query.pageNum;
    let pageSize = req.query.pageSize;
    let likeStr = ``;
    if (req.query.product_out_id) {
        likeStr = ` where product_out_id like '%${req.query.product_out_id}%'`;
    }
    const sql = `select * from outproduct` + likeStr + ` limit ${(pageNum - 1) * pageSize} ,${pageSize}`;
    db.query(sql, (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        const sql1 = `select count(*) as total from outproduct` + likeStr;
        db.query(sql1, (err, results1) => {
            // 执行 SQL 语句失败
            if (err) return res.cc(err);
            res.send({
                // 状态
                status: 0,
                // 状态描述，判断 err 是 错误对象 还是 字符串
                message: '查询成功',
                data: {
                    list: results,
                    total: results1[0].total
                }
            })
        })

    })
}


exports.withdraw = (req, res) => {

    const sql = `update product set product_out_id = null, product_out_number= null,product_out_price = null,product_out_apply_person =null,product_out_apply_date=null,apply_memo =null, product_out_status = null where id = ?`;
    db.query(sql, [req.query.id], (err, results) => {
        // 执行 SQL 语句失败
        if (err) return res.cc(err);
        // 执行 SQL 语句成功，但是影响行数不等于 1
        if (results.affectedRows !== 1) return res.cc('撤回申请失败！');
        res.cc('撤回申请成功', 0)

    })

}